package com.weipu.common.facade.model;

import java.util.ArrayList;
import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.util.Logger;

import static java.util.Locale.US;

/**
 * @Description  商品信息model
 * @version [版本号, 2012-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 *
 * @author yw66009
 */
public class ProductModel extends ProductBaseModel implements Parcelable
{

    private static final long serialVersionUID = 9130650492612599864L;

    /**
     * freeShipping类型
     */
    private int freeShipping;

    private String[] relateFreeShipping;

    /**
     * 语言
     */
    private String language;

    /**
     * 交易币种
     */
    private String currencyCode;
    /**
     * 货币符号 如$
     */
    private String currencyCodeSymbol;

    /**
     * 商品小图路径列表(只有详情为多张)
     */
    private String[] SmallPictureURL;

    /**
     * 商品中图路径列表
     */
    private String[] MediumPictureURL;

    /**
     * 商品大图路径列表
     */
    private String[] BigPictureURL;

    /**
     * 商品描述
     */
    private String introduction;

    /**
     * 商品规格
     */
    private String spec;

    /**
     * (本地)产品数据更新时间，用于本地数据缓存，单位：毫秒
     */
    public long cacheUpdateTime;

    public long getCacheUpdateTime()
    {
        return cacheUpdateTime;
    }

    public void setCacheUpdateTime(long cacheUpdateTime)
    {
        this.cacheUpdateTime = cacheUpdateTime;
    }

    /**
     * 照片数量
     */
    private int photosCount;

    private int EntityID;

    /**
     * 链接地址
     */
    private String webUrl = "";

    /**
     * 销售状态
     * 正常销售 Normal=1,
     * 预售PreSales=2,
     * 暂时停售（缺货等原因）PauseSales=3,
     * 下架 OffShelf = 4
     * US仓 status = 5
     */
    private int status;

    /**
     * @描述：产品标识
     * 0：普通产品
     * 1：MVP产品
     */
    private int tag;

    private float RawPrice;

    private boolean CanReturnSC;

    /**
     * US:30,SZ:10,HK:20
     */
    private int Warehouses;

    private boolean isShowUS;

    public int getWarehouses() {
        return Warehouses;
    }

    public void setWarehouses(int warehouses) {
        Warehouses = warehouses;
    }

    public boolean isShowUS() {
        return isShowUS;
    }

    public void setShowUS(boolean showUS) {
        isShowUS = showUS;
    }

    public float getRawPrice() {
        return RawPrice;
    }

    public void setRawPrice(float rawPrice) {
        RawPrice = rawPrice;
    }

    public boolean isCanReturnSC() {
        return CanReturnSC;
    }

    public void setCanReturnSC(boolean canReturnSC) {
        CanReturnSC = canReturnSC;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getCurrencyCodeSymbol() {
        return currencyCodeSymbol;
    }

    public void setCurrencyCodeSymbol(String currencyCodeSymbol) {
        this.currencyCodeSymbol = currencyCodeSymbol;
    }

    public String[] getSmallPictureURL()
    {
        return SmallPictureURL;
    }

    public void setSmallPictureURL(String[] smallPictureURL)
    {
        SmallPictureURL = smallPictureURL;
    }

    public String[] getBigPictureURL()
    {
        return BigPictureURL;
    }

    public void setBigPictureURL(String[] bigPictureURL)
    {
        BigPictureURL = bigPictureURL;
    }

    public String getLanguage()
    {
        return language;
    }

    public void setLanguage(String language)
    {
        this.language = language;
    }

    public String getCurrencyCode()
    {
        return currencyCode;
    }

    public void setCurrencyCode(String currencyCode)
    {
        this.currencyCode = currencyCode;
    }

    public String getIntroduction()
    {
        return introduction;
    }

    public void setIntroduction(String introduction)
    {
        this.introduction = introduction;
    }

    public String getSpec()
    {
        return spec;
    }

    public void setSpec(String spec)
    {
        this.spec = spec;
    }

    public int getPhotosCount()
    {
        return photosCount;
    }

    public void setPhotosCount(int photosCount)
    {
        this.photosCount = photosCount;
    }

    public String getWebUrl()
    {
        return webUrl;
    }

    public void setWebUrl(String webUrl)
    {
        this.webUrl = webUrl;
    }

    public String[] getMediumPictureURL()
    {
        return MediumPictureURL;
    }

    public void setMediumPictureURL(String[] mediumPictureURL)
    {
        MediumPictureURL = mediumPictureURL;
    }

    public int getEntityID()
    {
        return EntityID;
    }

    public void setEntityID(int entityID)
    {
        EntityID = entityID;
    }

    public int getFreeShipping()
    {
        return freeShipping;
    }

    public void setFreeShipping(int freeShipping)
    {
        this.freeShipping = freeShipping;
    }

    public String[] getRelateFreeShipping()
    {
        return relateFreeShipping;
    }

    public void setRelateFreeShipping(String[] relateFreeShipping)
    {
        this.relateFreeShipping = relateFreeShipping;
    }


    @Override
    public String toString() {
        return "ProductModel{" +
                "freeShipping=" + freeShipping +
                ", relateFreeShipping=" + Arrays.toString(relateFreeShipping) +
                ", language='" + language + '\'' +
                ", currencyCode='" + currencyCode + '\'' +
                ", currencyCodeSymbol='" + currencyCodeSymbol + '\'' +
                ", SmallPictureURL=" + Arrays.toString(SmallPictureURL) +
                ", MediumPictureURL=" + Arrays.toString(MediumPictureURL) +
                ", BigPictureURL=" + Arrays.toString(BigPictureURL) +
                ", introduction='" + introduction + '\'' +
                ", spec='" + spec + '\'' +
                ", cacheUpdateTime=" + cacheUpdateTime +
                ", photosCount=" + photosCount +
                ", EntityID=" + EntityID +
                ", webUrl='" + webUrl + '\'' +
                ", status=" + status +
                ", tag=" + tag +
                ", RawPrice=" + RawPrice +
                ", CanReturnSC=" + CanReturnSC +
                ", Warehouses=" + Warehouses +
                '}';
    }

    public static ProductModel json2product(String strJson)
    {
        try
        {
            return json2product(new JSONObject(strJson));
        }
        catch (JSONException e)
        {
            Logger.e("ProductModel.json2product", e);
        }
        return null;
    }

    /**
     * 商品对象解析
     * @param json 要解析的产品字符串
     * @return 本地产品对象
     */
    public static ProductModel json2product(JSONObject json)
    {
        if (null == json)
        {
            return null;
        }

        ProductModel model = new ProductModel();
        try
        {
            model.setSku(json.optInt(ServerConstant.Product.CONSTANT_SKU));
            if (json.optString(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1))
            {
                model.setShortHeadLine1(json.optString(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1));
            }
            if (json.optString(ServerConstant.Product.CONSTANT_LANGUAGE) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_LANGUAGE))
            {

                model.setLanguage(json.optString(ServerConstant.Product.CONSTANT_LANGUAGE));
            }

            if (json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE))
            {

                model.setCurrencyCode(json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE));
            }
            if (json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODESYMBOL) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODESYMBOL))
            {

                model.setCurrencyCodeSymbol(json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODESYMBOL));
            }
            model.setPrice((float)json.optDouble(ServerConstant.Product.CONSTANT_PRICE));
            model.setListprice((float)json.optDouble(ServerConstant.Product.CONSTANT_LISTPRICE));
            model.setEntityID(json.optInt(ServerConstant.Product.CONSTANT_ENTITYID));
            if (json.optString(ServerConstant.Product.CONSTANT_INTRODUCTION) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_INTRODUCTION))
            {
                model.setIntroduction(json.optString(ServerConstant.Product.CONSTANT_INTRODUCTION));
            }
            if (json.optString(ServerConstant.Product.CONSTANT_SPECIFICATIONS) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_SPECIFICATIONS))
            {
                model.setSpec(json.optString(ServerConstant.Product.CONSTANT_SPECIFICATIONS));
            }
            model.setReviewCount(json.optInt(ServerConstant.Product.CONSTANT_REVIEW_COUNT));
            model.setProductRating(json.optInt(ServerConstant.Product.CONSTANT_PRODUCT_RATING));
            model.setPhotosCount(json.optInt(ServerConstant.Product.CONSTANT_PHOTOSCOUNT));
            //            model.setReviewCount(json.optInt(ServerConstant.Product.CONSTANT_REVIEW_COUNT));
            if (json.optString(ServerConstant.Product.CONSTANT_WEBURL) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_WEBURL))
            {
                model.setWebUrl(json.optString(ServerConstant.Product.CONSTANT_WEBURL));
            }
            model.setStatus(json.optInt(ServerConstant.Product.CONSTANT_STATUS));
            model.setFreeShipping(json.optInt(ServerConstant.Product.CONSTANT_FREESHIPPING));

            JSONArray relateFS = json.optJSONArray(ServerConstant.Product.CONSTANT_RELATEFREESHIPPING);
            if (null != relateFS)
            {
                String[] relates = new String[relateFS.length()];
                if (null != relateFS && relateFS.length() != 0)
                {
                    for (int i = 0; i < relateFS.length(); i++)
                    {
                        relates[i] = relateFS.getString(i);
                    }
                }
                model.setRelateFreeShipping(relates);
            }

            //小图地址数组
            JSONArray smallPicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_SLIDE_SHOW_PICTURE_URLS);
            String[] smallUrls = null;
            if (null != smallPicArray && smallPicArray.length() > 0)
            {
                int len = smallPicArray.length();
                smallUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = smallPicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        smallUrls[i] = smallPicArray.getString(i);
                    }
                }
            }

            model.setSmallPictureURL(smallUrls);

            //中图地址数组
            JSONArray mediumPicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_MEDIUM_PICTURE_URLS);
            String[] mediumUrls = null;

            if (null != mediumPicArray && mediumPicArray.length() > 0)
            {
                int len = mediumPicArray.length();
                mediumUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = mediumPicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        mediumUrls[i] = mediumPicArray.getString(i);
                    }
                }
            }

            model.setMediumPictureURL(mediumUrls);

            //大图地址数组
            JSONArray largePicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_SLIDE_SHOW_PICTURE_THUMB_URLS);
            String[] imageLargerUrls = null;
            if (null != largePicArray && largePicArray.length() > 0)
            {
                int len = largePicArray.length();
                imageLargerUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = largePicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        imageLargerUrls[i] = largePicArray.getString(i);
                    }
                }
            }

            //US仓版本修改 新增字段 WareHouseTag
            model.setWarehouses(json.optInt(ServerConstant.Product.CONSTANT_WAREHOUSETAG));
//            model.setWarehouses(30);

            /* if (largePicArray == null || largePicArray.length() == 0)
             {
                 imageLargerUrls = new String[] { Constant.Html.DEFAULT_486_IMAGE_URL };
             }
             else
             {
                 int len = largePicArray.length();
                 imageLargerUrls = new String[len];
                 for (int i = 0; i < len; i++)
                 {
                     String tmpUrl = largePicArray.getString(i);
                     if (TextUtils.isEmpty(tmpUrl))
                     {
                         imageLargerUrls[i] = Constant.Html.DEFAULT_486_IMAGE_URL;
                     }
                     else
                     {
                         imageLargerUrls[i] = largePicArray.getString(i);
                     }
                 }
             }*/
            model.setBigPictureURL(imageLargerUrls);
            model.setTag(json.optInt("Tag"));
            model.setRawPrice((float) json.optDouble("RawPrice"));
            model.setCanReturnSC(json.optBoolean("CanReturnSC",false));
        }
        catch (Exception e)
        {
            Logger.e("ProductModel.json2product", e);
        }
        return model;
    }

    /**
     * JsonArray转化为ProductModel list
     * @param arrays
     * @return productList商品对象的list
     */
    public static ArrayList<ProductModel> jsonArray2Products(JSONArray arrays)
    {
        ArrayList<ProductModel> productList = null;
        if (null != arrays && arrays.length() > 0)
        {
            productList = new ArrayList<ProductModel>(arrays.length());
            for (int i = 0; i < arrays.length(); i++)
            {
                productList.add(ProductModel.json2product(arrays.optJSONObject(i)));
            }
        }
        return productList;
    }

    /**
     * 商品对象解析
     * @param json 要解析的产品字符串
     * @return 本地产品对象
     */
    public static ProductModel json3product(JSONObject json)
    {
        if (null == json)
        {
            return null;
        }
        ProductModel model = new ProductModel();
        try
        {
            model.setSku(json.optInt(ServerConstant.Product.CONSTANT_SKU));
            if (json.optString(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1))
            {
                model.setShortHeadLine1(json.optString(ServerConstant.Product.CONSTANT_SHORT_HEAD_LINE1));
            }

            if (json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_CURRENCYCODE))
            {

                model.setCurrencyCode(json.optString(ServerConstant.Product.CONSTANT_CURRENCYCODE));
            }
            model.setPrice((float)json.optDouble(ServerConstant.Product.CONSTANT_PRICE));
            model.setListprice((float)json.optDouble(ServerConstant.Product.CONSTANT_LISTPRICE));
            if (json.optString(ServerConstant.Product.CONSTANT_SHORTDESCRIPTION) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_SHORTDESCRIPTION))
            {
                model.setIntroduction(json.optString(ServerConstant.Product.CONSTANT_SHORTDESCRIPTION));
            }
            if (json.optString(ServerConstant.Product.CONSTANT_SPECIFICATIONS) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_SPECIFICATIONS))
            {
                model.setSpec(json.optString(ServerConstant.Product.CONSTANT_SPECIFICATIONS));
            }
            model.setReviewCount(json.optInt(ServerConstant.Product.CONSTANT_REVIEW_COUNT));
            model.setProductRating(json.optInt(ServerConstant.Product.CONSTANT_AVGRATING));
            if (json.optString(ServerConstant.Product.CONSTANT_WEBURL) != JSONObject.NULL
                    && !json.isNull(ServerConstant.Product.CONSTANT_WEBURL))
            {
                model.setWebUrl(json.optString(ServerConstant.Product.CONSTANT_WEBURL));
            }
            model.setStatus(json.optInt(ServerConstant.Product.CONSTANT_STATUS));

            //US仓版本修改 新增字段需要解析
//            model.setWareHouseTag(json.optInt(ServerConstant.Product.CONSTANT_WAREHOUSETAG));

            //小图地址数组
            JSONArray smallPicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_GETSMALLIMAGES);
            String[] smallUrls = null;
            if (null != smallPicArray && smallPicArray.length() > 0)
            {
                int len = smallPicArray.length();
                smallUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = smallPicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        smallUrls[i] = smallPicArray.getString(i);
                    }
                }
            }

            model.setSmallPictureURL(smallUrls);

            //中图地址数组
            JSONArray middlePicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_GETMEDIUMIMAGES);
            String[] middleUrls = null;
            if (null != middlePicArray && middlePicArray.length() > 0)
            {
                int len = middlePicArray.length();
                middleUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = middlePicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        middleUrls[i] = middlePicArray.getString(i);
                    }
                }
            }

            model.setMediumPictureURL(middleUrls);

            //大图地址数组
            JSONArray largePicArray = json.optJSONArray(ServerConstant.Product.CONSTANT_GETBIGIMAGES);
            String[] imageLargerUrls = null;
            if (null != largePicArray && largePicArray.length() > 0)
            {
                int len = largePicArray.length();
                imageLargerUrls = new String[len];

                for (int i = 0; i < len; i++)
                {
                    String tmpUrl = largePicArray.getString(i);

                    if (null != tmpUrl && tmpUrl.length() > 0)
                    {
                        imageLargerUrls[i] = largePicArray.getString(i);
                    }
                }
            }
            model.setBigPictureURL(imageLargerUrls);
            model.setTag(json.optInt("Tag"));

            //US仓版本修改 新增字段 WareHouseTag
            model.setWarehouses(json.optInt(ServerConstant.Product.CONSTANT_WAREHOUSETAG));
//            model.setWarehouses(30);
        }
        catch (Exception e)
        {
            Logger.e("ProductModel.json2product", e);
        }
        return model;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeString(language);
        parcel.writeString(currencyCode);

        parcel.writeInt(freeShipping);

        if (null==relateFreeShipping) {
            parcel.writeInt(0);
        } else {
            parcel.writeInt(relateFreeShipping.length);
        }
        if(null!=relateFreeShipping){
            parcel.writeStringArray(relateFreeShipping);
        }

        if (null==SmallPictureURL) {
            parcel.writeInt(0);
        } else {
            parcel.writeInt(SmallPictureURL.length);
            parcel.writeStringArray(SmallPictureURL);
        }
        if (null==MediumPictureURL) {
            parcel.writeInt(0);
        } else {
            parcel.writeInt(MediumPictureURL.length);
            parcel.writeStringArray(MediumPictureURL);
        }
        if (null==BigPictureURL) {
            parcel.writeInt(0);
        } else {
            parcel.writeInt(BigPictureURL.length);
            parcel.writeStringArray(BigPictureURL);
        }


        parcel.writeString(introduction);
        parcel.writeString(spec);
        parcel.writeLong(cacheUpdateTime);
        parcel.writeInt(photosCount);
        parcel.writeInt(EntityID);
        parcel.writeString(webUrl);

        parcel.writeInt(sku);
        parcel.writeString(shortHeadLine1);
        parcel.writeFloat(price);
        parcel.writeInt(quantity);
        parcel.writeFloat(listprice);
        parcel.writeInt(reviewCount);
        parcel.writeFloat(productRating);
        parcel.writeInt(status);
        parcel.writeInt(tag);
        //US仓版本修改 序列化读取
        parcel.writeInt(Warehouses);

    }

    public ProductModel() {

    }

    public ProductModel(Parcel source) {

        language = source.readString();
        currencyCode = source.readString();
        freeShipping = source.readInt();
        int length=source.readInt();
        if(length>0){
            relateFreeShipping=new String[length];
            source.readStringArray(relateFreeShipping);
        }

        int smallPicLength=source.readInt();
        if(smallPicLength>0){
            SmallPictureURL=new String[smallPicLength];
            source.readStringArray(SmallPictureURL);
        }
        int mediumPicLength=source.readInt();
        if(mediumPicLength>0){
            MediumPictureURL=new String[mediumPicLength];
            source.readStringArray(MediumPictureURL);
        }
        int bigPicLength=source.readInt();
        if(bigPicLength>0){
            BigPictureURL=new String[bigPicLength];
            source.readStringArray(BigPictureURL);
        }

        introduction = source.readString();
        spec = source.readString();
        cacheUpdateTime = source.readLong();
        photosCount = source.readInt();
        EntityID = source.readInt();
        webUrl = source.readString();

        sku=source.readInt();
        shortHeadLine1=source.readString();
        price=source.readFloat();
        quantity=source.readInt();
        listprice=source.readFloat();
        reviewCount=source.readInt();
        productRating=source.readFloat();
        status = source.readInt();
        tag = source.readInt();
        //US仓版本修改 序列化读取
        Warehouses = source.readInt();
    }

    public static final Parcelable.Creator<ProductModel> CREATOR = new Creator<ProductModel>(){

        @Override
        public ProductModel createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new ProductModel(source);
        }

        @Override
        public ProductModel[] newArray(int size) {
            // TODO Auto-generated method stub
            return new ProductModel[size];
        }

    };

    /**
     * SKU所属仓库标识
     */
    public static final class WarehouseTag{
        /**
         * 美国仓
         */
        public static final int US = 30;

        /**
         * 深圳仓
         */
        public static final int SZ = 10;

        /**
         * 香港仓
         */
        public static final int HK = 20;


    }

}
